// pages/address/address.js
import WxValidate from "../../utils/wxValidate.js";
var api = require("../../utils/api");


Page({
  /**
   * 页面的初始数据
   */
  data: {
    addrs: [],
    // 默认
    addrMr: [],
    //提交订单页面传参
    show: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.addrs();
    this.setData({
      show: options.type,
    })
  },
  addrs() {
    let addrs = this.data.addrs;
    api.handleAddr({
      openId: wx.getStorageSync("openId"),
      cityId: wx.getStorageSync("cityId"),
      addressType:1
    })
      .then(res => {
        let arr = res.object;
        let addrMr = []
        arr.forEach((v, i) => {
          if (v.cityId == wx.getStorageSync("cityId")) {
            //城市相同
            addrMr.push(v)
          }else if (v.flag == 1 && v.cityId != wx.getStorageSync("cityId")) {
            //把其他城市的flag设为0
            v.flag = 0
          }
        })
        if (res.result && res.object.length != 0) {
          this.setData({
            addrs: res.object,
            addrMr,
          })
        }
      })
  },
  edit: function () {
    wx.navigateTo({
      url: '../type/type?newAdd=1',
    })
  },
  addrShop(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    if (this.data.show == 1) {
      let pages = getCurrentPages(e);
      let prevPage = pages[pages.length - 2];
      let addrs = that.data.addrs;
      let addrMess = null;
      addrs.forEach((v, i) => {
        if (i === index) {
          addrMess = {
            buildNo: v.buildNo,//门牌号
            addr: v.addr,//地址
            cityCode: v.cityCode,
            takePerson: v.takePerson,//收货人
            linktel: v.linktel,//电话
            shopId: v.shopId,//店铺id 
            shopName: v.shopName,//店铺名称
            latitude: v.latitude,//纬度
            longitude: v.longitude//经度
          }
        }
      });
      prevPage.setData({
        addrMess
      })
      wx.navigateBack({
        delta: 1
      })
    } else {
      let shopid = e.currentTarget.dataset.shopid;
      let addrid = e.currentTarget.dataset.addrid;
      wx.navigateTo({
        url: '/pages/shopBack/shopBack?shopId=' + shopid + "&addrid=" + addrid,
      })
    }
  }
})